package com.hwd.srb.sms.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.hwd.srb.common.exception.BusinessException;
import com.hwd.srb.common.util.RandomUtils;
import com.hwd.srb.common.constant.SmsConstants;
import com.hwd.srb.sms.service.SmsService;
import com.hwd.srb.sms.util.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 黄伟东/Victor
 * @date 2022/3/16 9:32
 * @Description
 */
@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    /**
     * 短信验证码过期时间 5 minutes
     */
    public static final long REDIS_CODE_KEY_TIMEOUT_MILLISECONDS = 5 * 60 * 1000;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 发送验证码短信
     *
     * @param mobile 手机号
     */
    @Override
    public void send(String mobile) {

        // 1、配置公共参数
        DefaultProfile profile = DefaultProfile.getProfile(SmsProperties.REGION_ID, SmsProperties.KEY_ID, SmsProperties.KEY_SECRET);

        // 2、api客户端
        IAcsClient iAcsClient = new DefaultAcsClient(profile);

        // 3、请求参数封装
        CommonRequest commonRequest = new CommonRequest();
        commonRequest.setSysMethod(MethodType.POST);
        commonRequest.setSysDomain(SmsConstants.DOMAIN);
        commonRequest.setSysVersion(SmsConstants.VERSION_2017_05_25);
        commonRequest.setSysAction(SmsConstants.ACTION_SEND_SMS);
        commonRequest.putQueryParameter(SmsConstants.REGION_ID, SmsProperties.REGION_ID);
        commonRequest.putQueryParameter(SmsConstants.PHONE_NUMBERS, mobile);
        commonRequest.putQueryParameter(SmsConstants.SIGN_NAME, SmsProperties.SIGN_NAME);
        commonRequest.putQueryParameter(SmsConstants.TEMPLATE_CODE, SmsProperties.TEMPLATE_CODE);

        // 生成验证码
        String sixBitRandom = RandomUtils.getSixBitRandom();

        // 封装短信内容
        Map<String, String> map = new HashMap<>();
        map.put(SmsConstants.CODE_KEY, sixBitRandom);
        String json = new Gson().toJson(map);
        commonRequest.putQueryParameter(SmsConstants.TEMPLATE_PARAM, json);

        // 4、客户端发送请求
        try {
            CommonResponse commonResponse = iAcsClient.getCommonResponse(commonRequest);
            boolean success = commonResponse.getHttpResponse().isSuccess();
            // 5、处理返回结果
            if (success) {
                String key = String.format(SmsConstants.REDIS_CODE_KEY_FORMAT, mobile);
                // 将经验证码存入redis中，默认有效时间为五分钟
                redisTemplate.opsForValue().set(key, sixBitRandom, REDIS_CODE_KEY_TIMEOUT_MILLISECONDS);
            }
        } catch (ClientException e) {
            log.error(SmsConstants.SEND_SMS_ERROR);
            log.error("异常信息:{}", e.getMessage());
            e.printStackTrace();
            throw new BusinessException(SmsConstants.SEND_SMS_ERROR);
        }

    }

    /**
     * 给手机号发送消息
     *
     * @param mobile  手机号
     * @param message 消息
     */
    @Override
    public void notifyMessage(String mobile, String message) {

        // 发送提示短信

        System.out.println(mobile + message);
    }
}
